package com.shujia.scala

import scala.collection.mutable
import scala.collection.mutable.HashSet

object Demo17Set {

  def main(args: Array[String]): Unit = {


    /**
      * set 无序，唯一，默认也是不可变
      *
      */

    val sets = Set(1, 2, 3, 4, 5, 6, 1, 12, 13, 21, 4123)

    println(sets)


    var s1: Set[Int] = Set(1, 2, 3, 4, 5, 6, 7)
    var s2: Set[Int] = Set(4, 5, 6, 7, 8, 9, 10)

    println(s1)
    println(s2)

    println(s1 & s2) //交集
    println(s1 | s2) //并集
    println(s1 &~ s2) //差集


    val s3 = Set(1, 2, 3, 4, 5, 6, 7, 8, 9)

    println(s3.filter(i => i % 2 == 1))
    println(s3.map(i => i * 2))

    println(s3.max)
    println(s3.min)
    println(s3.head)
    println(s3.last)
    println(s3.mkString(","))


    /**
      * 可变set
      *
      */

    val hashSet = new mutable.HashSet[String]()

    //增加元素
    hashSet.add("java")

    hashSet += "spark"

    println(hashSet)

    //删除元素
    hashSet.remove("java")

    hashSet -= "spark"
    println(hashSet)
  }

}
